﻿#region Copyright (c) 2010 Active Web Solutions Ltd
//
// (C) Copyright 2010 Active Web Solutions Ltd
//      All rights reserved.
//
// This software is provided "as is" without warranty of any kind,
// express or implied, including but not limited to warranties as to
// quality and fitness for a particular purpose. Active Web Solutions Ltd
// does not support the Software, nor does it warrant that the Software
// will meet your requirements or that the operation of the Software will
// be uninterrupted or error free or that any defects will be
// corrected. Nothing in this statement is intended to limit or exclude
// any liability for personal injury or death caused by the negligence of
// Active Web Solutions Ltd, its employees, contractors or agents.
//
#endregion

using System;
using System.Configuration;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.IO;

namespace DownloadBlob
{
    /// <summary>
    /// Downloads a file from Windows Azure Blob Storage to local disk
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            string blobPath = "";
            string outputDirectory = ".";

            if (args.Length == 1)
            {
                blobPath = args[0];
            }
            else if (args.Length == 2)
            {
                blobPath = args[0];
                outputDirectory = args[1];
            } 
            else
            {
                Console.WriteLine("Usage: DownloadBlob container\\blob [outputdirectory]");
                return;
            }

            string accountName = Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT");
            if (accountName == null)
                accountName = ConfigurationManager.AppSettings["AccountName"];

            string key = Environment.GetEnvironmentVariable("AZURE_STORAGE_KEY");
            if (key == null)
                key = ConfigurationManager.AppSettings["Key"];

            StorageCredentialsAccountAndKey storageCredentialsAccountAndKey = new StorageCredentialsAccountAndKey(accountName, key);
            CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentialsAccountAndKey, false);

            string[] fields = blobPath.Split('\\');
            string containerName = fields[0];
            string blobName = fields[1];

            CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();

            blobClient.RetryPolicy = RetryPolicies.Retry(100, TimeSpan.FromSeconds(1));
            blobClient.Timeout = TimeSpan.FromSeconds(600);

            // Use a retry policy suitable for crappy, low bandwidth, rural UK ADSL broadband connections
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

            blob.DownloadToFile(outputDirectory + "\\" + blobName);

        }
    }

}
